﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QUAN_LY_NGAN_HANG_CAU_HOI_TRAC_NGHIEM
{
    public partial class FrmThayDoiThongTinGiaoVien : Form,ICapNhatGiaoVien
    {
        private TaiKhoanDAL taikhoan;
        private GiaoVienDAL giaovien;
        private CapNhatGiaoVienPresenter p = new CapNhatGiaoVienPresenter();
        private bool DoiPass;
       
        public FrmThayDoiThongTinGiaoVien(TaiKhoanDAL tk)
        {
            InitializeComponent();
            p.Init(this);
            taikhoan = tk;
        }

        private void linkThoat_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            this.Close();
        }

        private void FrmThemMoiGiaoVien_Load(object sender, EventArgs e)
        {
            giaovien = p.LayThongTinGiaoVien(taikhoan.Username);
            txtHoGV.Text = giaovien.HoGiaoVien;
            txtTenGV.Text = giaovien.TenGiaoVien;
            txtDiaChi.Text = giaovien.TenGiaoVien;
            txtCMND.Text = giaovien.CMND;
            txtNgaySinh.Text = giaovien.NgaySinh.ToShortDateString();
            txtDienThoai.Text = giaovien.DienThoai;
            txtPass.Text = taikhoan.Password;
            txtUser.Text = taikhoan.Username;
            DoiPass = false;
            this.Left = 50;
            this.Top = 0;

            this.helpProvider1.SetHelpNavigator(this, HelpNavigator.Topic);
            this.helpProvider1.SetHelpKeyword(this, "ToMiDThi.html");
        }

        private void linkThemGiaoVien_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if(DoiPass==true)
            {
                if(txtPass.Text != txtRePass.Text)
                {
                    MessageBox.Show("Mật khẩu gõ lại không đúng", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }

            GiaoVienDAL giaovien = new GiaoVienDAL();
            giaovien.HoGiaoVien = txtHoGV.Text;
            giaovien.TenGiaoVien = txtTenGV.Text ;
            giaovien.TenGiaoVien =  txtDiaChi.Text;
            giaovien.CMND = txtCMND.Text;
            giaovien.DiaChi = txtDiaChi.Text;
            DateTime ngaysinh;
            if (DateTime.TryParse(txtNgaySinh.Text, out ngaysinh) == true)
                giaovien.NgaySinh = ngaysinh;
            giaovien.DienThoai = txtDienThoai.Text;
            TaiKhoanDAL tam = new TaiKhoanDAL();
            tam.Password = txtPass.Text;
            tam.Username = taikhoan.Username;
            GiaoVienBUS.CapNhatGiaoVien(giaovien,tam);
            MessageBox.Show("Cập nhật thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

       

        private void linkXoaGiaoVien_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            giaovien = p.LayThongTinGiaoVien(taikhoan.Username);
            txtHoGV.Text = giaovien.HoGiaoVien;
            txtTenGV.Text = giaovien.TenGiaoVien;
            txtDiaChi.Text = giaovien.TenGiaoVien;
            txtCMND.Text = giaovien.CMND;
            txtNgaySinh.Text = giaovien.NgaySinh.ToShortDateString();
            txtDienThoai.Text = giaovien.DienThoai;
            txtPass.Text = taikhoan.Password;
            txtUser.Text = taikhoan.Username;
            DoiPass = false;
        }

        private void linkDoiMatKhau_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            labelRepass.Enabled = true;
            txtRePass.Enabled = true;
            DoiPass = true;
            txtPass.ReadOnly = false;
        }
    }
}
